package com.yinhe.music.yhmusic.music;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.yinhe.music.type.person.BaseInfo;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.model.MvsInfo;
import com.yinhe.music.yhmusic.network.NetworkManager;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.network.schedulers.SchedulerProvider;
import com.yinhe.music.yhmusic.utils.NetworkUtils;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.utils.ToastUtils;
import com.yinhe.music.yhmusic.video.VideoPlayingActivity;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author 何晓华
 */
public class PlayUtils {

    public interface CallBack {
        void getMusicUrlSuccess(Music song);
    }

    public static void getMusicPlayUrl(Music music, CallBack callBack) {

        CompositeDisposable mDisposable = new CompositeDisposable();

        Disposable disposable = NetworkManager.getWebService().getMusicUrl((int) music.getSongId(), music.getQuality())
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerProvider.getInstance().applySchedulers())
                .subscribe(baseInfo -> {
                    music.setUrl(baseInfo.getPlayUrl());
                    callBack.getMusicUrlSuccess(music);
                }, throwable -> {
                    ToastUtils.show(throwable.getMessage());
                });

        mDisposable.add(disposable);
    }

    public static void getRadioPlayUrl(Music music, CallBack callBack) {

        CompositeDisposable mDisposable = new CompositeDisposable();

        Disposable disposable = NetworkManager.getWebService().getRadioSongUrl((int) music.getSongId())
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerProvider.getInstance().applySchedulers())
                .subscribe(baseInfo -> {
                    music.setUrl(baseInfo.getPlayUrl());
                    callBack.getMusicUrlSuccess(music);
                }, throwable -> {
                    ToastUtils.show(throwable.getMessage());
                });

        mDisposable.add(disposable);
    }

    public static Disposable getMvInfo(int mvId, Context context) {
        return NetworkManager.getWebService().getMvInfo(mvId)
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerProvider.getInstance().applySchedulers())
                .subscribe(mvsInfo -> getMvPlayUrl(mvsInfo, context)
                , throwable -> ToastUtils.show(throwable.getMessage()));
    }

    public static void getMvPlayUrl(MvsInfo mvsInfo, Context context) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("提示");
        dialog.setMessage("检测到没有连接wifi，是否继续");
        dialog.setPositiveButton("确定", (dialogInterface, i) -> playMvIsConnectWiFi(mvsInfo, context));
        dialog.setNegativeButton("取消", (dialogInterface, i) -> ToastUtils.show("请连接wifi后重试"));

        if (!NetworkUtils.isConnectWifi(context) && Preferences.getIsProtect()) {
            dialog.show();
        } else {
            playMvIsConnectWiFi(mvsInfo, context);
        }
    }

    private static void playMvIsConnectWiFi(MvsInfo mvsInfo, Context context) {
        CompositeDisposable mDisposable = new CompositeDisposable();
        Disposable disposable = NetworkManager.getWebService().getMvUrl(mvsInfo.getMovieId())
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerProvider.getInstance().applySchedulers())
                .subscribe(baseInfo -> playMv(baseInfo, mvsInfo, context),
                        throwable -> ToastUtils.show(throwable.getMessage()));
        mDisposable.add(disposable);
    }

    private static void playMv(BaseInfo baseInfo, MvsInfo mvsInfo, Context context) {
        Intent intent = new Intent(context, VideoPlayingActivity.class);
        intent.putExtra(VideoPlayingActivity.VIDEO_URL, baseInfo.getPlayUrl());
        intent.putExtra("mv", mvsInfo);
        context.startActivity(intent);
    }

    /*
    * 用户听歌记录
    * */
    public static void recordMusic(Music music) {
        CompositeDisposable mDisposable = new CompositeDisposable();

        Disposable disposable = NetworkManager.getWebService().recordUserSong((int) music.getSongId())
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerProvider.getInstance().applySchedulers())
                .subscribe(baseInfo -> {}, throwable -> {});

        mDisposable.add(disposable);
    }
}
